﻿#region using
using System;
using AwesomeTanks.Sources.GameElements.TankElements;
using Microsoft.Xna.Framework;
using AwesomeTanks.Sources.PlayerInputs;
using AwesomeTanks.Sources.Screens;
#endregion

namespace AwesomeTanks.Sources.GameElements.Factories
{
    /// <summary>
    /// The factory of Players. This class returns an instance of a Player
    /// </summary>
    public static class PlayerFactory
    {
        #region Fields
        public static int INITIAL_TANK_LIFE = 100;
        public static int INITIAL_PLAYER_LIFE = OptionsMenuScreen.CurrentLife;
        #endregion

        /// <summary>
        /// Creates the player with his TeamColor and his PlayerIndex.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="teamcolor">The teamcolor.</param>
        /// <param name="indexPlayer">The index player.</param>
        /// <returns></returns>
        public static Player CreatePlayer(String name,TeamColor teamcolor, PlayerIndex indexPlayer)
        {
            Tank tank = new Tank(INITIAL_TANK_LIFE);
            GameplayScreen.listTank.Add(tank);
            //Assign a Color to the Player
            Color playerColor = GetPlayerColor(teamcolor, indexPlayer);
            tank.Turret.Color = playerColor;
            tank.Position = GameplayScreen.map.getSpawn(indexPlayer);
            //Assign the Gamepad to the Player    
            GamePadControlsState gamepad = new GamePadControlsState(indexPlayer);
            return new Player(name, teamcolor, playerColor, tank, gamepad,OptionsMenuScreen.lifes[OptionsMenuScreen.CurrentLife],indexPlayer);
        }

        /// <summary>
        /// Creates the debug player.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="teamcolor">The teamcolor.</param>
        /// <param name="indexPlayer">The index player.</param>
        /// <returns></returns>
        public static Player CreateDebugPlayer(String name, TeamColor teamcolor, PlayerIndex indexPlayer)
        {
            Tank tank = new Tank(INITIAL_TANK_LIFE);
            GameplayScreen.listTank.Add(tank);
            Color playerColor = GetPlayerColor(teamcolor, indexPlayer);
            tank.Turret.Color = playerColor;
            tank.Position = GameplayScreen.map.getSpawn(indexPlayer);
            KeyboardControlsState keyboard = new KeyboardControlsState(tank);
            return new Player(name, teamcolor, playerColor, tank, keyboard, OptionsMenuScreen.lifes[OptionsMenuScreen.CurrentLife], indexPlayer);
        }

        #region Methods
        /// <summary>
        /// Returns the color of the player according to his TeamColor and his PlayerIndex.
        /// </summary>
        /// <param name="teamcolor">The teamcolor.</param>
        /// <param name="indexplayer">The indexplayer.</param>
        /// <returns></returns>
        private static Color GetPlayerColor(TeamColor teamcolor, PlayerIndex indexplayer)
        {
            Color playerColor = Color.Black;
            switch (teamcolor)
            {
                case TeamColor.BLUE:
                    playerColor = (indexplayer == PlayerIndex.One) ? Color.DarkTurquoise : Color.SlateBlue; 
                    break;
                case TeamColor.RED:
                    playerColor = (indexplayer == PlayerIndex.Two) ? Color.Salmon : Color.OrangeRed; 
                    break;
                case TeamColor.GREEN:
                    playerColor = Color.Lime;
                    break;
                case TeamColor.YELLOW:
                    playerColor = Color.Yellow;
                    break;
            }
            return playerColor;
        }

        #endregion
    }
}
